<?php

namespace app\applet\controller;

use think\Validate;

class Goods
{
    public function __construct()
    {
        $this->goods = new \app\applet\model\Goods();
    }
    //zlc-商铺返利商品分类
    public function rebateClass()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '商品ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->rebateClass($input['id']);
        returnJson(200,"success",$data);

    }
    //zlc-商铺首页附近联盟商家商铺展示
    public function homePageGoodsStore()
    {
        $input = input();
        $validate = new Validate([
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->goods->homePageGoodsStore($input,$current_page,$page_num);
        returnJson(200,"success",$data);

    }
    //zlc-民脉首页附近联盟商家商铺展示
    public function homePageGoods()
    {
        $input = input();
        $validate = new Validate([
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->goods->homePageGoods($input,$current_page,$page_num);
        returnJson(200,"success",$data);

    }
    //店招支付
    public function shopPayment()
    {
        $input = input();
        $validate = new Validate([
            ['goods_id', 'require', '商品ID不能为空'],
            ['total_fee', 'require', '店招合计费用不能为空'],
            ['start_time', 'require', '店招开始时间不能为空'],
            ['end_time', 'require', '店招结束时间不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id("token");

        $data = $this->goods->shopPayment($input);
        returnJson(200,"success",$data);

    }

    /**
     * 取消支付
     */
    public function cancelPay(){
        $input = input();
        $validate = new Validate([
            ['order_sn', 'require', '支付单号不能为空'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id']= get_header_id("token");

        $returnData=$this->goods->cancelOrder($input);
        returnJson(200,'success',$returnData);

    }

    //设置店招
    public function recommendSet()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '商品ID不能为空'],
            ['type', 'require', 'API类别不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $msg = $this->goods->recommendSet($input);
        returnJson(200,$msg);

    }
    public function pointsAwardConfig()
    {
        $data = $this->goods->pointsAwardConfig();
        returnJson(200,"success",$data);
    }
    //商铺商品分类详情
    public function shopGoodsClassDetail()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '分类ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->shopGoodsClassDetail($input['id']);
        returnJson(200,"success",$data);

    }
    //商铺商品分类列表显示
    public function shopGoodsClassList()
    {
        $member_id = get_header_id("token");
        $data = $this->goods->shopGoodsClassList($member_id);
        returnJson(200,"success",$data);

    }
    //zlc-商铺商品分类删除
    public function shopGoodsClassDel()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '分类ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id("token");
        $this->goods->shopGoodsClassDel($input);
        returnJson(200,"success");
    }
    //商铺商品分类添加和编辑
    public function shopGoodsClass()
    {
        $input = input();
        $validate = new Validate([
            ['name', 'require|max:20', '分类名称不能为空|分类名称长度不能超过20'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id("token");
        $this->goods->shopGoodsClass($input);
        returnJson(200,"success");
    }
    #返利商品发布和编辑
    public function rebateSave()
    {
        $input = input();
        $validate = new Validate([
            ['gc_id', 'require', '分类不能为空'],
            ['g_name', 'require|max:50', '商品名称不能为空|商品名称长度不能超过50'],
            ['cover_pic', 'require', '商品主图不能为空'],
            ['specifications', 'require', '商品规格不能为空'],
//            ['points_award', 'require', '积分奖不能为空'],
//            ['is_gold_coin', 'require', '是否消费送金币不能为空'],
            ['limit_num_status', 'require', '是否限购不能为空'],
            ['limit_time_status', 'require', '是否限时不能为空'],
            ['distribution_method', 'require', '配送方式不能为空'],
            ['delivery_type', 'require', '送货上门方式不能为空'],
            ['is_change', 'require', '7天可换不能为空'],
            ['is_real', 'require', '7天可换不能为空'],
            ['is_change', 'require', ' 正品承诺不能为空'],
            ['is_diff', 'require', ' 差价返回不能为空'],
            ['product_details_image', 'require', ' 商品详情图不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id("token");


        #添加商品到数据库
        $bool = $this->goods->rebateSave($input);
        if($bool)
        {
            returnJson(200,'success');
        }else{
            returnJson(-2004,'error');
        }
    }
    //zlc-商家讲坛的发布和编辑
    public function forumSave()
    {
        $input = input();
        $validate = new Validate([
            ['type', 'require', '分类不能为空'],
            ['g_name', 'require|max:50', '课程名称不能为空|课程名称长度不能超过50'],
            ['video_path', 'require', '课程地址不能为空'],
            ['free_time', 'require', '免费时间不能为空'],
            ['cost_price', 'require', '原价不能为空'],
            ['price', 'require', '单价不能为空'],
            ['product_details_image', 'require', '课程介绍图片不能为空'],
            ['proportion', 'require', '商家和平台的结算比例不能为空'],
//            ['points_award', 'require', '积分奖配置不能为空'],
//            ['is_gold_coin', 'require', '是否消费送金币不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id("token");


        #添加商品到数据库
        $bool = $this->goods->forumSave($input);
        if($bool)
        {
            returnJson(200,'success');
        }else{
            returnJson(-2004,'error');
        }

    }
    //商品详情
    public function goodsDetail()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '商品ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $member_id = get_header_id("token");
        if(isset($input['invt_id'])&&!empty($input['invt_id'])&&!empty($member_id)){
            $this->goods->goodsMember($input['id'],$input['invt_id'],$member_id);
        }
        $data = $this->goods->goodsDetail($input['id']);
        returnJson(200,"success",$data);
    }
    //zlc-联盟商家商品收藏
    public function goodsCollection()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '商品ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data['id'] = $this->goods->goodsCollection($input['id']);
        returnJson(200,"收藏成功",$data);
    }
    //zlc-联盟商家商铺收藏
    public function shopCollection()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '店铺ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data['id'] = $this->goods->shopCollection($input['id']);
        returnJson(200,"收藏成功",$data);
    }
    //返利
    public function listGoodsRebateCate()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '店铺ID不能为空'],
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->goods->listGoodsRebateCate($input,$current_page,$page_num);
        returnJson(200,"success",$data);

    }
    //返利
    public function listGoodsRebate()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '店铺ID不能为空'],
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->listGoodsRebate($input);
        returnJson(200,"success",$data);
    }
    //zlc-商铺首页展示--商家讲坛
    public function listGoodsForumCate()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '店铺ID不能为空'],
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
            ['type', 'require', 'type不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->goods->listGoodsForumCate($input,$current_page,$page_num);
        returnJson(200,"success",$data);

    }

    //zlc-商铺首页展示--商家讲坛
    public function listGoodsForum()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '店铺ID不能为空'],
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
            ['type', 'require', 'type不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->listGoodsForum($input);
        returnJson(200,"success",$data);

    }
    //zlc-商铺首页商铺展示
    public function listGoodsFirst()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '店铺ID不能为空'],
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->listGoodsFirst($input);
        returnJson(200,"success",$data);

    }
    //商铺首页备注
    public function storeNote()
    {
        $data = $this->goods->storeNote();
        returnJson(200,"success",$data);
    }
    //zlc-根据行业分类查找店铺
    public function storeList()
    {
        $input = input();
        $validate = new Validate([
            ['type', 'require', '行业分类不能为空'],
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->storeList($input);
        returnJson(200,"success",$data);
    }
    //商铺首页轮播
    public function shopsPic()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '商铺ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->shopsPic($input['id']);
        returnJson(200,"success",$data);
    }
    //zlc-民脉首页顶部和底部轮播
    public function advert()
    {
        $input = input();
        $validate = new Validate([
            ['type', 'require', 'type不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->advert($input['type']);
        returnJson(200,"success",$data);
    }
    //zlc-民脉首页公告
    public function notice()
    {
        $data = $this->goods->notice();
        returnJson(200,"success",$data);
    }

    //zlc-附近联盟商家首页分类接口
    public function goodsClass()
    {
        $data = $this->goods->goodsClass();
        returnJson(200,"success",$data);
    }
    //zlc-商品添加其他联盟 商家广告
    public function whetherShop()
    {
        $input = input();
        $validate = new Validate([
            ['phone', 'require', '商家注册手机号不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->whetherShop($input['phone']);
        returnJson(200,"success",$data);

    }
    //zlc-限时特惠商品的发布和编辑
    public function limitSave()
    {
        $input = input();
        $validate = new Validate([
            ['g_name', 'require|max:50', '活动名称不能为空|活动名称长度不能超过50'],
            ['cover_pic', 'require', '活动主图不能为空'],
            ['free_gift', 'require', '免费赠品特权图不能为空'],
            ['consumption_pic', 'require', '消费特权图不能为空'],
            ['cost_price', 'require', '总价值不能为空'],
            ['registration_fee', 'require', '报名费不能为空'],
            ['direct_push', 'require', '直推分享金不能为空'],
            ['inter_push', 'require', '间推分享金不能为空'],
            ['limit_registration_num', 'require', '每人报名次数限制不能为空'],
            ['activity_start_time', 'require', '活动开始时间不能为空'],
            ['activity_end_time', 'require', '活动结束时间不能为空'],
            ['distribution_method', 'require', '配送方式不能为空'],
            ['delivery_type', 'require', '送货上门类型不能为空'],
            ['is_change', 'require', '七天可换承诺不能为空'],
            ['is_real', 'require', '正品承诺不能为空'],
            ['is_diff', 'require', '价格最低承诺不能为空'],
            ['product_details_image', 'require', '商品详情图不能为空'],

        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id("token");


        #添加商品到数据库
        $bool = $this->goods->limitSave($input);
        if($bool)
        {
            returnJson(200,'success');
        }else{
            returnJson(-2004,'error');
        }
    }

    //zlc-裂变商品的发布和编辑
    public function fissionSave()
    {
        $input = input();
        $validate = new Validate([
            ['g_name', 'require|max:50', '商品名称不能为空|商品名称长度不能超过50'],
            ['cover_pic', 'require', '商品主图或者视频不能为空'],
            ['specifications', 'require', '商品规格不能为空'],
            ['limit_num_status', 'require', '是否限量不能为空'],
            ['limit_time_status', 'require', '是否限时不能为空'],
            ['distribution_method', 'require', '配送方式不能为空'],
            ['delivery_type', 'require', '送货上门类型不能为空'],
            ['product_details_image', 'require', '商品详情图不能为空'],
            ['copper_money', 'require', '铜客进货金额不能为空'],
            ['copper_pop', 'require', '铜客分享积分奖不能为空'],
            ['silver_money', 'require', '银客进货金额不能为空'],
            ['silver_pop', 'require', '银客分享积分奖不能为空'],
            ['gold_money', 'require', '金客进货金额不能为空'],
            ['gold_pop', 'require', '金客分享积分奖不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id("token");

        #添加商品到数据库
        $bool = $this->goods->fissionSave($input);
        if($bool)
        {
            returnJson(200,'success');
        }else{
            returnJson(-2004,'error');
        }
    }
    //zlc-联盟商家消费爆品首页商品展示
    public function explosiveList()
    {
        $input = input();
        $validate = new Validate([
            ['id', 'require', '店铺ID不能为空'],
            ['type', 'require', 'type不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->goods->explosiveList($current_page,$page_num,$input);
        returnJson(200,'success',$data);

    }
    //消费爆品管理查看详情
    public function explosiveShowDetail()
    {
        $input = input();
        $validate = new Validate([
            ['goods_id', 'require', '商品ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $data = $this->goods->explosiveShowDetail($input['goods_id']);
        returnJson(200,"success",$data);
    }
    //下架商品删除,
    public function shopDel()
    {
        $input = input();
        $validate = new Validate([
            ['goods_id', 'require', '商品ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $this->goods->shopDel($input['goods_id']);
        returnJson(200,"商品删除成功");
    }
    /**
     *设置店招
     */
    public function setShop()
    {

    }
    /**
     *商品上架或者下架,使用所有商品的上下架
     */
    public function isShow()
    {
        $input = input();
        $validate = new Validate([
            ['status', 'require', '状态不能为空'],
            ['goods_id', 'require', '商品ID不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $msg = $this->goods->isShow($input);
        returnJson(200,$msg);
    }
    /**
     * 消费爆品管理展示
     */
    public function explosiveShow()
    {
        $input = input();
        $validate = new Validate([
            ['status', 'require', '状态不能为空'],
            ['type', 'require', '类型不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $type = $input['type'];
        $is_type = empty($input['is_type']) ? 0 : $input['is_type'];
        $where['status'] = $input['status'];
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->goods->explosiveShow($current_page,$page_num,$where,$type,$is_type);
        returnJson(200,'success',$data);
    }
    /**
     * 消费爆品发布
     */
    public function explosiveProducts()
    {
        $input = input();
        $validate = new Validate([
            ['g_name', 'require|max:50', '商品名称不能为空|商品名称长度不能超过50'],
            ['cover_pic', 'require', '商品主图或者视频不能为空'],
            ['specifications', 'require', '商品规格不能为空'],
            ['limit_num_status', 'require', '是否限量不能为空'],
            ['limit_time_status', 'require', '是否限时不能为空'],
            ['distribution_method', 'require', '配送方式不能为空'],
            ['delivery_type', 'require', '送货上门类型不能为空'],
            ['is_change', 'require', '七天可换承诺不能为空'],
            ['is_real', 'require', '正品承诺不能为空'],
            ['is_diff', 'require', '价格最低承诺不能为空'],
            ['product_details_image', 'require', '商品详情图不能为空'],
//            ['advertisement', 'require', '联盟商家广告不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $input['member_id'] = get_header_id("token");


        #添加商品到数据库
        $bool = $this->goods->explosiveProducts($input);
        if($bool)
        {
            returnJson(200,'success');
        }else{
            returnJson(-2004,'error');
        }
    }

    /**
     * 精选
     */
    public function carefullyChosen(){
        $input=input();
        $validate = new Validate([
            ['type', 'require', '精选类型不能为空'],
            ['longitude', 'require', '经度不能为空'],
            ['latitude', 'require', '纬度不能为空'],
        ]);
        if(!$validate->check($input))
        {
            returnJson(-2003,$validate->getError());
        }
        $current_page = empty($input['current_page']) ? 1 : $input['current_page'];
        $page_num = empty($input['page_num']) ? 10: $input['page_num'];
        $data = $this->goods->carefullyChosen($current_page,$page_num,$input);
        returnJson(200,'success',$data);
    }

}